package com.sen.gulimall.cart.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * @author Kisen
 * @email lqs0911@qq.com
 * @date 2022/11/3 22:43
 * @detail
 */
@EnableRedisHttpSession
@Configuration
public class GulimallSessionConfig {

    /**
     * Once you have setup Spring Session you can easily customize how the session cookie is written by exposing a
     * CookieSerializer as a Spring Bean. Out of the box, Spring Session comes with DefaultCookieSerializer. Simply
     * exposing the DefaultCookieSerializer as a Spring Bean will augment the existing configuration when using
     * configurations like @EnableRedisHttpSession.
     * (一旦您设置了Spring Session，就可以通过将CookieSerializer公开为Spring Bean来
     * 轻松定制会话cookie的编写方式。开箱即用，Spring Session附带了
     * DefaultCookieSerializer。当使用@EnableRedisHttpSession等配置时，简单地将
     * DefaultCookieSerializer公开为SpringBean将增加现有配置。)
     *
     * @return
     */
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookieName("JSESSIONID");
        serializer.setDomainName("gulimall.com"); //解决子域session共享问题
        return serializer;
    }

    /**
     * 自定义SpringSession Redis序列化器
     * Custom RedisSerializer
     * You can customize the serialization by creating a Bean named springSessionDefaultRedisSerializer that
     * implements RedisSerializer<Object>.
     *
     * @return
     */
    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }
}
